package com.novanok.campaigntweets;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class ActivityPalabra extends Activity {

    private static final String TAG = "ActivityPalabra";
    public static Palabra palabraSeleccionada = null;

    private Campania campaniaSeleccionada = null;

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView(R.layout.activity_palabra);

        this.campaniaSeleccionada = MyActivity.campaniaSeleccionada;

        if ( this.campaniaSeleccionada != null ) {
            (( TextView ) findViewById( R.id.lblCampania )).setText( "Palabras de la Campaña ( " + this.campaniaSeleccionada.getNombre() + ")" );
            (( TextView ) findViewById( R.id.lblCampaniaDescripcion )).setText( this.campaniaSeleccionada.getDescripcion() );
        }

        setClickLista();
    }

    public void onResume(){
        super.onResume();
        this.campaniaSeleccionada = MyActivity.campaniaSeleccionada;

        if ( this.campaniaSeleccionada != null && this.campaniaSeleccionada.getPalabras() != null ) {
            (( ListView ) ActivityPalabra.this.findViewById( R.id.lstPalabras )).setAdapter(
                    new ListaPalabraAdapter( ActivityPalabra.this, this.campaniaSeleccionada.getPalabras() ) );
        }
    }

    @Override
    public boolean onCreateOptionsMenu( Menu menu ) {
        getMenuInflater().inflate( R.menu.menu_tweets, menu );
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch ( item.getItemId() ) {
            case R.id.nuevo_tweet:
                this.nuevoPalabra();
                return true;

            case R.id.editar_tweet:
                this.editarPalabra();
                return true;

            case R.id.eliminar_tweet:
                this.eliminarPalabra();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void eliminarPalabra (){
        ListaPalabraAdapter adapter = ( ListaPalabraAdapter )( ( ListView ) findViewById( R.id.lstPalabras ) ).getAdapter();
        if ( palabraSeleccionada != null ) {
            this.campaniaSeleccionada.getPalabras().remove( palabraSeleccionada );
            adapter.notifyDataSetChanged();

            Log.d(TAG, "Eliminacion de Palabra");
        }
    }

    public void nuevoPalabra(){
        Intent i = new Intent(this, ActivityPalabraItem.class);
        i.putExtra("palabra_accion", "nuevo");
        startActivity(i);
    }

    public void editarPalabra(){
        Intent i = new Intent(this, ActivityPalabraItem.class);
        i.putExtra("palabra_accion", "editar");
        startActivity(i);
    }

    private void setClickLista(){
        ListView lv = ( ListView )findViewById( R.id.lstPalabras );

        lv.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick( AdapterView<?> adapterView, View view, int i, long l ) {
                view.setSelected( true );

                ListaPalabraAdapter adapter = ( ListaPalabraAdapter )( ( ListView ) findViewById( R.id.lstPalabras ) ).getAdapter();
                Palabra toRemove = adapter.getItem( ( ( ListView ) findViewById( R.id.lstPalabras ) ).getPositionForView( view ) );
                ActivityPalabra.palabraSeleccionada = toRemove;

                Log.d( TAG, "Click Item Lista " + toRemove.getDescripcion() );
            }
        });
    }
}
